package com.xyzq.cgate.core.condition.strategy.concrete;

import com.google.common.base.Preconditions;
import com.xyzq.cgate.common.rule.custom.entity.MatchCondition;
import com.xyzq.cgate.core.condition.strategy.AbstractConditonMatchStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;

/**
 * @Description: 或代表：module必须相等，其余三个条件
 * @author chenchen
 * @date 2019/6/20 8:53
 */
public class OrConditionMatchStrategy extends AbstractConditonMatchStrategy {

    @Override
    public Boolean match(MatchCondition requestCondition, MatchCondition ruleCondition) {
        try {
            Preconditions.checkNotNull(requestCondition);
            Preconditions.checkNotNull(ruleCondition);
            Preconditions.checkArgument(StringUtils.isNoneBlank(requestCondition.getModule())
                    && StringUtils.isNoneBlank(ruleCondition.getModule())
                    && StringUtils.equalsIgnoreCase(requestCondition.getModule(), ruleCondition.getModule()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return new AntPathMatcher().match(ruleCondition.getMethod(), requestCondition.getMethod())
                || new AntPathMatcher().match(ruleCondition.getIp(), requestCondition.getIp())
                || StringUtils.equalsIgnoreCase(requestCondition.getQueryType(), ruleCondition.getQueryType());
    }
}
